package cn.tedu.xuecha.service.impl;

import cn.tedu.xuecha.entity.dto.ContentDTO;
import cn.tedu.xuecha.entity.pojo.Content;
import cn.tedu.xuecha.entity.pojo.ContentDetail;
import cn.tedu.xuecha.ex.ServiceException;
import cn.tedu.xuecha.mapper.ContentDetailMapper;
import cn.tedu.xuecha.mapper.ContentMapper;
import cn.tedu.xuecha.respone.AppCode;
import cn.tedu.xuecha.service.ContentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author coinyang
 * @date 2023/4/23 10:53
 */
@Service
public class ContentServiceImpl implements ContentService {

    @Autowired
    ContentMapper contentMapper;
    @Autowired
    ContentDetailMapper contentDetailMapper;


    public ContentDetail selectView(Integer id){
        return contentDetailMapper.selectByPrimaryKey(id);
    }

    @Override
    public void addContent(ContentDTO contentDTO) {
        if(contentDTO ==null){
            String message = "新增文章不能为空";
            throw new ServiceException( AppCode.ERR_CONFLICT,message);
        }
        Content content = new Content();
        BeanUtils.copyProperties(contentDTO,content);
        content.setId(2);
        int rows = contentMapper.insert(content);
        if(rows == 0 ){
            String message = "文章新增失败";
            throw  new ServiceException(AppCode.ERR_INSERT,message);
        }
    }

    public List<Content> list(){
        return contentMapper.list();
    }
    @Override
    public void updateContent(ContentDTO contentDTO,Integer id) {
        if(contentDTO ==null){
            String message = "文章不能为空";
            throw new ServiceException( AppCode.ERR_CONFLICT,message);
        }
        Content content = new Content();
        BeanUtils.copyProperties(contentDTO,content);
        content.setId(id);
        int rows = contentMapper.updateByPrimaryKeySelective(content);
        if(rows == 0 ){
            String message = "文章更新失败";
            throw  new ServiceException(AppCode.ERR_INSERT,message);
        }
    }
}
